package com.gpay.erp.web.security;

import com.gpay.erp.web.vo.ResponseInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2015/5/28 0028.
 */
public class LoginInterceptor implements HandlerInterceptor {
    private Log log = LogFactory.getLog(LoginInterceptor.class);
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        String parentPath=request.getRequestURI();
        HttpSession session=request.getSession();
//        ResponseInfo responseInfo = (ResponseInfo)session.getAttribute("responseInfo");
        // 登陆页面无需过滤
        if(parentPath.indexOf("/erp/loginController/login.html") > -1) {
                     return true;
                 }
        Object userName = session.getAttribute("loginUser");
        if(userName == null){
//            if(null==request.getSession(false)){
//                if(true==request.getSession(true).isNew()){
//                }
//                else{
//                    System.out.println("session已经过期");
//                    request.getRequestDispatcher("loginPage.html").forward(request,response);
//                    return false;
//                }
//            }
            log.info("Interceptor：跳转到login页面！");
            ResponseInfo responseInfo = new ResponseInfo();
            responseInfo.setMessage("");
            request.setAttribute("responseInfo",responseInfo);
            request.getRequestDispatcher("/WEB-INF/jsp/timeOut.jsp").forward(request, response);
            return false;
        }

        return true;
    }
    /**
     * 在业务处理器处理请求执行完成后,生成视图之前执行的动作
     */
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }
    /**
     * 在DispatcherServlet完全处理完请求后被调用
     *   当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     */
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}